package org.bbs.demo.user.api;


import org.bbs.demo.common.BBSConstants;
import org.bbs.demo.common.Result;
import org.bbs.demo.user.dao.UserInfoDao;
import org.bbs.demo.user.dto.UserInfoDto;
import org.bbs.demo.utils.jwtutil.JWTutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Transactional //事务声明，表示该类的所有方法都是事务性的，即方法中的所有访问数据库操作构成了一个整体事务，具有原子性
public class UserInfoApi {
    @Autowired
    private UserInfoDao userinfodao;

    @PostMapping("/info")
    public Result info(@RequestHeader(BBSConstants.HEADER_PARAM_TOKEN) String token){
        UserInfoDto userInfoDto = userinfodao.findUserInfo(JWTutil.getUserID(token));
        if (userInfoDto == null){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"用户id不存在");
        }

        return Result.OK("读取成功", userInfoDto);
    }
}
